package com.listeningframework.boot.commons.httpclient;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.apache.http.message.BasicHeader;

import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * HttpClient Headers
 *
 * @author Listening
 * @version $Id: HttpClientHeaders.java, v 0.1 2016年8月6日 上午11:54:26 Listening Exp $
 */
public final class HttpClientHeaders {
    /**
     * HttpClientHeaders
     */
    private static final HttpClientHeaders INSTANCE = new HttpClientHeaders();
    /**
     * 重定向CODE
     */
    private final Set<Integer> DEFAULT_REDIRECTS = Sets.newConcurrentHashSet();
    /**
     * Http Headers
     */
    private final Map<String, Header> DEFAULT_HEADERS = Maps.newConcurrentMap();
    /**
     * Agents
     */
    private final Set<String> DEFAULT_AGENTS = Sets.newConcurrentHashSet();

    /**
     * Setting Header And Agents Src
     */
    private HttpClientHeaders() {
        // ---------------------- Http Redirect Codes -----------------------------
        DEFAULT_REDIRECTS.clear();
        DEFAULT_REDIRECTS.add(HttpStatus.SC_MOVED_TEMPORARILY);
        DEFAULT_REDIRECTS.add(HttpStatus.SC_MOVED_PERMANENTLY);
        DEFAULT_REDIRECTS.add(HttpStatus.SC_SEE_OTHER);
        DEFAULT_REDIRECTS.add(HttpStatus.SC_TEMPORARY_REDIRECT);

        // ---------------------- Http Headers ------------------------------------
        // @formatter:off
        DEFAULT_HEADERS.clear();
        String accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
        addHeader(HttpHeaders.ACCEPT, accept);
        addHeader(HttpHeaders.ACCEPT_LANGUAGE, "zh-cn,en-us,zh-tw,en-gb,en;");
        addHeader(HttpHeaders.ACCEPT_CHARSET, "zh-CN,zh;q=0.8");
        addHeader(HttpHeaders.CONNECTION, "keep-alive");
        addHeader(HttpHeaders.CACHE_CONTROL, "max-age=0");
        addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate, br");
        addHeader(HttpHeaders.PRAGMA, "no-cache");

        // ---------------------- Http Agents ------------------------------------
        DEFAULT_AGENTS.clear();
        // IE
        DEFAULT_AGENTS.add("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
        DEFAULT_AGENTS.add("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)");
        DEFAULT_AGENTS.add("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        DEFAULT_AGENTS.add("Mozilla/4.0 (compatible; MSIE 5.0; Windows NT)");
        // Firefox
        DEFAULT_AGENTS.add("Mozilla/5.0 (Windows; U; Windows NT 5.2) Gecko/2008070208 Firefox/3.0.1 ");
        DEFAULT_AGENTS.add("Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070309 Firefox/2.0.0.3 ");
        DEFAULT_AGENTS.add("Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12");
        DEFAULT_AGENTS.add("Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0)");
        DEFAULT_AGENTS.add("Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0");
        // Opera
        DEFAULT_AGENTS.add("Opera/9.27 (Windows NT 5.2; U; zh-cn)");
        DEFAULT_AGENTS.add("Opera/8.0 (Macintosh; PPC Mac OS X; U; en)");
        DEFAULT_AGENTS.add("Mozilla/5.0 (Macintosh; PPC Mac OS X; U; en) Opera 8.0");
        // Safari
        DEFAULT_AGENTS.add("Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13");
        DEFAULT_AGENTS.add("Mozilla/5.0 (iPhone; U; CPU like Mac OS X) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3");
        // Chrome
        DEFAULT_AGENTS.add("Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13");
        DEFAULT_AGENTS.add("Mozilla/5.0 (Windows NT 5.2) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30");
        DEFAULT_AGENTS.add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");
        // Navigator
        DEFAULT_AGENTS.add("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080219 Firefox/2.0.0.12 Navigator/9.0.0.6");
        // 360极速浏览器
        DEFAULT_AGENTS.add("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ;  QIHU 360EE)");
        // @formatter:on
    }

    private void addHeader(String name, String value) {
        DEFAULT_HEADERS.put(name, new BasicHeader(name, value));
    }

    /**
     * Get User Agent
     *
     * @return String
     */
    public static final String getUserAgent() {
        Set<String> agents = INSTANCE.DEFAULT_AGENTS;
        Integer index = new Random().nextInt(agents.size() - 1);
        return Lists.newArrayList(agents).get(index);
    }

    /**
     * Get Direct Codes
     *
     * @return Set
     */
    public static final Set<Integer> getRedirectCodes() {
        return INSTANCE.DEFAULT_REDIRECTS;
    }

    /**
     * Get Headers
     *
     * @return Map
     */
    public static final Map<String, Header> getHeaders() {
        return INSTANCE.DEFAULT_HEADERS;
    }

    /**
     * Is Redirect Code
     *
     * @param code 随机数
     * @return boolean
     */
    public static final boolean isRedirect(Integer code) {
        return INSTANCE.DEFAULT_REDIRECTS.contains(code);
    }

}
